# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-TODAY OpenERP S.A. <http://www.openerp.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.addons.dm_base.utils import field_boolean_name_cn as boolean_name,field_selection_name as selection_name, dtstr_utc2local
from openerp.addons.dm_rubylong import get_rubylong_fields_xml,get_rublong_data_url
from openerp.osv import fields, osv
from datetime import datetime
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT

class stock_move(osv.osv):
    _inherit="stock.move"
    
    def _get_rubylong_xml_qc(self, cr, uid, ids, field_names, args, context=None):
        if isinstance(ids, (int, long)):
            ids = [ids]
        res = {}
        for order_id in ids:
            res[order_id] = ''
        data_xml = ''
        for order in self.browse(cr, uid, ids, context=context):
            #当前对象的xml
            data_xml = ''
            #header data
            order_fields = [
                        ('id', 'order_id'),
                        ('company_id.logo','company_logo'),
                        ('company_id.name','company_id_name'),
#                        #公司地址
                        ('company_id.street','company_id_street'),
                        ('company_id.street2','company_id_street2'),
                        ('company_id.city','company_id_city'),
                        ('company_id.state_id.name','company_id_state'),
                        ('company_id.country_id.name','company_id_country_name'),
                        #电话,传真
                        ('company_id.phone','company_id_phone'),
                        ('company_id.fax','company_id_fax'),
                        
                        #质检信息
                        {'to_qc_date':dtstr_utc2local(cr, uid, order.picking_id.to_qc_date, context=context)},
                        ('pick_partner_id.name','partner_id'),
                        {'qc_date':dtstr_utc2local(cr, uid, order.qc_date, context=context)},
                        ('product_id.default_code','product_code'),
                        ('product_id.name','product_name'),
                        ('product_id.name','product_id'),
                        ('product_id.variants','variants'),
                        'qc_pass',
                        'product_qty',
                        'qc_qty',
                        'qc_qty_check',
                        'qc_qty_defect',
                        'qc_qty_good',
                        'qc_range',
                        ('qc_user_id.name','qc_user_id')
                        ]
            
            order_fields += [{'qc_pass':boolean_name(order.qc_pass)},
                             {'qc_range':selection_name(self, cr, uid, 'stock.move','qc_range',order.qc_range,context=context)}]
            data_xml += get_rubylong_fields_xml(order, 'header', order_fields)
            
            #质检项目
            line_fields = [('move_id', 'order_id'),
                           ('quality_item_id.name','quality_item_id'),
                           ('importance.name','importance'),
                           'item_name',
                           'check_standard',
                           'check_method',
                           'check_tools',
                           'result_standard',
                           ('check_quality','check_quatity'),
                           'check_result',
                           'defect_quality',
                           'note']
            seq = 0
            for line in order.move_quality_item_ids:
                seq += 1
                line_fields_new = line_fields + [{'sequence':seq},
                           {'item_type':selection_name(line, cr, uid, 'quality.item.move','item_type',line.item_type,context=context)}]
                data_xml += get_rubylong_fields_xml(line, 'detail', line_fields_new)

            #write data to file            
            res[order.id] = get_rublong_data_url(order,data_xml,cr.dbname)
            
        return res            

    _columns = {
            'rubylong_xml_file_qc_in':fields.function(_get_rubylong_xml_qc, type='text', string='Rubylong xml'),
            'purchase_id':fields.related('picking_id','purchase_id',type='many2one',relation='purchase.order',string='采购订单')
            }
    
#po_super.STATE_SELECTION = STATE_SELECTION_PO  